package com.carbohydrate42.kotlin_compose_demo.viewModel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.carbohydrate42.kotlin_compose_demo.config.RoomConfig
import com.carbohydrate42.kotlin_compose_demo.constant.Common
import com.carbohydrate42.kotlin_compose_demo.entity.User
import kotlinx.coroutines.launch

class TryRoomOrmViewModel : ViewModel() {
    var dataBaseStr: String by mutableStateOf("")
        private set

    fun createOneUser() {
        viewModelScope.launch {
            RoomConfig.testDatabase.userDao().save(
                User(
                    id = null,
                    name = "name1",
                    password = "password1",
                    authToken = "authToken1"
                )
            )

            val users = RoomConfig.testDatabase.userDao().findAll()

            Log.d(Common.ANDROID_LOG_TAG.label, users.toString())

            dataBaseStr = users.toString()
            Log.d(Common.ANDROID_LOG_TAG.label, dataBaseStr)
        }
    }

    override fun onCleared() {
        super.onCleared()

        Log.d(Common.ANDROID_LOG_TAG.label, "onCleared")
    }
}